import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterroom/pages/home/widgets/common_image.dart';
import 'package:image_picker/image_picker.dart';
import 'package:multi_image_picker/multi_image_picker.dart';

const List<String> defautImages = [
  'http://ww3.sinaimg.cn/large/006y8mN6ly1g6e2tdgve1j30ku0bsn75.jpg',
  'http://ww3.sinaimg.cn/large/006y8mN6ly1g6e2whp87sj30ku0bstec.jpg',
  'http://ww3.sinaimg.cn/large/006y8mN6ly1g6e2tl1v3bj30ku0bs77z.jpg',
];

class RoomImagePicker extends StatefulWidget {
  final ValueChanged<List<File>> onCheng;

  const RoomImagePicker({Key key, this.onCheng}) : super(key: key);
  @override
  _RoomImagePickerState createState() => _RoomImagePickerState();
}

class _RoomImagePickerState extends State<RoomImagePicker> {
// 选择多找图片
// _muliImage() async{
// var resultList = await MultiImagePicker.pickImages(
//         // 选择图片的最大数量
//         maxImages: 9,
//         // 是否支持拍照
//         enableCamera: true,
//         materialOptions: MaterialOptions(
//             // 显示所有照片，值为 false 时显示相册
//             startInAllView: true,
//             allViewTitle: '所有照片',
//             actionBarColor: '#2196F3',
//             textOnNothingSelected: '没有选择照片'
//         ),
//       );

// }

  List<File> files = [];
  _pickImage() async {
    var image = await ImagePicker.pickImage(
        source: ImageSource.gallery, imageQuality: 6);
    if (image != null) {
      setState(() {
        files = files..add(image);
      });
      if (widget.onCheng != null) {
        widget.onCheng(files);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    var width = (ScreenUtil().setWidth(750) - 41) / 3;
    var height = width;
    var w = (MediaQuery.of(context).size.width);
    Widget addButton = GestureDetector(
      onTap: () {
        _pickImage();
      },
      behavior: HitTestBehavior.translucent,
      child: Container(
        width: width,
        height: height,
        color: Colors.grey,
        child: Center(
          child: Text(
            "+",
            style: TextStyle(fontWeight: FontWeight.w900, fontSize: 40),
          ),
        ),
      ),
    );

    Widget wrapper(File imgUrl) {
      return Stack(
        overflow: Overflow.visible,
        children: <Widget>[
          Image.file(
            imgUrl,
            width: width,
            height: height,
            fit: BoxFit.cover,
          ),
          Positioned(
              right: -20,
              top: -20,
              child: IconButton(
                icon: Icon(Icons.delete_forever),
                color: Colors.red,
                onPressed: () {
                  setState(() {
                    files = files..remove(imgUrl);
                  });
                  if (widget.onCheng != null) {
                    widget.onCheng(files);
                  }
                },
              ))
        ],
      );
    }

    ;

    List<Widget> list = files.map((item) => wrapper(item)).toList();
    if (files.length < 6) {
      list = list..add(addButton);
    }

    return Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: Wrap(
        spacing: 10,
        runSpacing: 10,
        children: list,
      ),
    );
  }
}
